using System;
using System.Threading.Tasks;
using Admin.NET.Application.Entity.Business;
using Admin.NET.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace Admin.NET.Application.Service.Mp;

/// <summary>
/// 小程序电表相关服务
/// </summary>
[ApiDescriptionSettings(new string[] { "业务应用" }, Order = 100)]
public class MpMeterService : IDynamicApiController, ITransient, IPrivateDependency
{
	private readonly SqlSugarRepository<ElectricMeter> _rep;

	private readonly SqlSugarRepository<PowerConsumptionRecord> _repQuery;

	public MpMeterService(SqlSugarRepository<ElectricMeter> rep, SqlSugarRepository<PowerConsumptionRecord> repQuery)
	{
		_rep = rep;
		_repQuery = repQuery;
	}

	/// <summary>
	/// 电表详细信息
	/// </summary>
	/// <param name="sn"></param>
	/// <returns></returns>
	[HttpGet("/mp/meter/info")]
	public async Task<MeterOutPut> MeterInfo(string sn)
	{
		return await (from u in _rep.AsQueryable()
			where u.Sn == sn
			select new MeterOutPut
			{
				SignalStatus = u.SignalStatus,
				Sn = u.Sn,
				CombinedActiveSharpElectricEnergy = u.CombinedActiveSharpElectricEnergy,
				GateStatus = u.GateStatus,
				Addr = u.Addr,
				CollectorId = u.CollectorId,
				CombinedActiveFlatElectricEnergy = u.CombinedActiveFlatElectricEnergy,
				CombinedActivePeakElectricEnergy = u.CombinedActivePeakElectricEnergy,
				CombinedActiveTotalElectricEnergy = u.CombinedActiveTotalElectricEnergy,
				CombinedActiveValleyElectricEnergy = u.CombinedActiveValleyElectricEnergy,
				TenantId = u.TenantId,
				CreateTime = u.CreateTime,
				CreateUserId = u.CreateUserId,
				Id = u.Id,
				IsDelete = u.IsDelete,
				Multiplier = u.Multiplier,
				Name = u.Name,
				PriceRuleId = u.PriceRuleId,
				PriceName = from s4 in SqlFunc.Subqueryable<ElectricityPriceRule>()
					where (long?)s4.Id == u.PriceRuleId
					select s4.Name,
				ProtocolType = u.ProtocolType,
				RoomId = u.RoomId,
				RoomName = from s3 in SqlFunc.Subqueryable<Room>()
					where (long?)s3.Id == u.RoomId
					select s3.Name,
				UpdateTime = u.UpdateTime,
				UpdateUserId = u.UpdateUserId
			}).FirstAsync();
	}

	/// <summary>
	/// 电表本月与上月的用电量环比
	/// </summary>
	/// <param name="sn"></param>
	/// <returns></returns>
	[HttpGet("/mp/meter/ringratio")]
	public async Task<dynamic> GetMeterRingRatio(string sn)
	{
		DateTime now = DateTime.Now;
		DateTime monthBegin = DateTime.Parse($"{now.Year}-{now.Month}-1");
		DateTime beforeMonthBegin = monthBegin.AddMonths(-1);
		DateTime beforNow = now.AddMonths(-1);
		decimal nowTotal = await (from w in _repQuery.Context.Queryable<PowerConsumptionRecord>()
			where w.Sn == sn && w.CreateTime >= monthBegin && w.CreateTime < now
			select w).SumAsync((PowerConsumptionRecord w) => w.CombinedActiveTotalElectricEnergyDifference);
		decimal beforeTotal = await (from w in _repQuery.Context.Queryable<PowerConsumptionRecord>()
			where w.Sn == sn && w.CreateTime >= beforeMonthBegin && w.CreateTime < beforNow
			select w).SumAsync((PowerConsumptionRecord w) => w.CombinedActiveTotalElectricEnergyDifference);
		decimal totalQOQ = default(decimal);
		if (beforeTotal != 0m)
		{
			totalQOQ = (nowTotal - beforeTotal) / beforeTotal * 100m;
		}
		return new { nowTotal, beforeTotal, totalQOQ };
	}
}
